ENT.Type = "point"
ENT.Base = "base_point"

ENT.SpawnParam = { }
ENT.SpawnParam["DefaultSettings"] = { Type = "slave", HealthMultiplier = 1.0, MaxAlive = 1, Amount = 10, Interval = 5.0, Radius = 128, StartEnabled = true }

ENT.SpawnVals = { }
ENT.SpawnVals["DefaultSettings"] = { SpawnCount = 0, ActiveZombies = { } }

function ENT:TimerID()
	return "zombiespawn_" .. self:EntIndex()
end

function ENT:GetSpawnParam(ParamKey)
	if(ParamKey == nil) then
		return self.SpawnParam[self:EntIndex()]
	else
		return self.SpawnParam[self:EntIndex()][ParamKey]
	end
end
function ENT:GetSpawnVals(ParamKey)
	if(ParamKey == nil) then
		return self.SpawnVals[self:EntIndex()]
	else
		return self.SpawnVals[self:EntIndex()][ParamKey]
	end
end

function ENT:ResetSpawnVals()
	self.SpawnVals[self:EntIndex()] = table.Copy(self.SpawnVals["DefaultSettings"])
end

function ENT:KeyValue(key, value)
	if( self.SpawnParam[self:EntIndex()] == nil) then
		self.SpawnParam[self:EntIndex()] = table.Copy(self.SpawnParam["DefaultSettings"])
	end
	
	if( key == "zombie_type" ) then
		self.SpawnParam[self:EntIndex()]["Type"] 					= string.Trim(string.lower(value))
	elseif( key == "zombie_health" ) then
		self.SpawnParam[self:EntIndex()]["HealthMultiplier"]	= math.Clamp(tonumber(value), 0.1, 4.0)
	elseif( key == "spawn_max_alive" ) then
		self.SpawnParam[self:EntIndex()]["MaxAlive"]			= math.Clamp(tonumber(value), 1, 16)
	elseif( key == "spawn_count" ) then
		self.SpawnParam[self:EntIndex()]["Amount"]				= math.Clamp(tonumber(value), 1, 1000)
	elseif( key == "spawn_interval" ) then
		self.SpawnParam[self:EntIndex()]["Interval"]				= math.Clamp(tonumber(value), 0.5, 300)
	elseif( key == "spawn_radius" ) then
		self.SpawnParam[self:EntIndex()]["Radius"]				= math.Clamp(tonumber(value), 0, 1024)
	elseif( key == "spawn_startenabled" ) then
		self.SpawnParam[self:EntIndex()]["StartEnabled"]	= (string.Trim(string.lower(value)) == "true")
	end
end

function ENT:ConfirmDead( zombie )
	for k,v in pairs(self.SpawnVals[self:EntIndex()].ActiveZombies) do
		if(v == zombie) then
			table.remove(self.SpawnVals[self:EntIndex()].ActiveZombies, k) break
		end
	end
end

function ENT:CreateZombie()
	if(#self:GetSpawnVals("ActiveZombies") < self:GetSpawnParam("MaxAlive")) then
	
		local zombie = ents.Create( "npc_zombie2" ) //  -- change to "zombie_" .. self.GetSpawnParam()["Type"] to add types
		
		local sPos = self:GetPos()
		local sAng = math.random(0,360)
		local sDist = math.random(0, self:GetSpawnParam()["Radius"])
		
		sPos = Vector(sPos.x + math.sin(sAng) * sDist, sPos.Y + math.cos(sAng) * sDist, sPos.z - 1)
		
		zombie:SetPos(sPos)
		zombie:Spawn()
		
		local oldhealth = zombie:GetMaxHealth()
		zombie:SetMaxHealth(oldhealth * self:GetSpawnParam()["HealthMultiplier"])
		zombie:SetHealth(zombie:GetMaxHealth())
		
		local tList = team.GetPlayers( 1 ) // 1 = TEAM_ALIVE
		for k,v in ipairs(tList) do
			if(v:Alive() == false) then table.remove(tList, k) end
		end
		
		if(false) then
			if( #tList > 0 ) then
				local target = tList[math.random(1, #tList)]
				zombie:AddRelationship(target:GetClass() .. " D_HT 99")
				zombie:SetEnemy(target)
			end
		end
		
		self.SpawnVals[self:EntIndex()].SpawnCount = self.SpawnVals[self:EntIndex()].SpawnCount + 1
		if(self:GetSpawnVals()["SpawnCount"] >= self:GetSpawnParam()["Amount"]) then
			self:StopSpawning()
		end
		
		table.insert(self.SpawnVals[self:EntIndex()].ActiveZombies, zombie)
		
	end
end

function ENT:Initialize()
	if(self.SpawnVals[self:EntIndex()] == nil) then
		self.SpawnVals[self:EntIndex()] = table.Copy(self.SpawnVals["DefaultSettings"])
	end

	timer.Adjust("zombiespawn_" .. self:EntIndex(), self:GetSpawnParam()["Interval"], 0, self.CreateZombie, self)
end

function ENT:StartSpawning()
	timer.Adjust(self:TimerID(), self:GetSpawnParam()["Interval"], 0, self.CreateZombie, self)
	timer.Start(self:TimerID())
end

function ENT:StopSpawning()
	timer.Stop(self:TimerID())
end